package com.rykj.qxj.interceptor;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rykj.qxj.server.common.utils.CollectionUtils;
import com.rykj.qxj.server.common.utils.StringUtils;
import com.techindata.cacheredis.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.rykj.qxj.server.common.constants.LevelEnum;
import com.rykj.qxj.server.model.entity.ApiManage;
import com.rykj.qxj.server.model.entity.Apply;
import com.rykj.qxj.server.model.entity.SystemParameter;
import com.rykj.qxj.server.services.IAddFieldService;
import com.rykj.qxj.server.services.IApiManageService;
import com.rykj.qxj.server.services.IApiRequestParamService;
import com.rykj.qxj.server.services.IApiResponseParamService;
import com.rykj.qxj.server.services.IApiVersionManageService;
import com.rykj.qxj.server.services.IApplyManageService;
import com.rykj.qxj.server.services.IDatabaseSourceService;
import com.rykj.qxj.server.services.ISystemParameterService;
import com.rykj.qxj.server.services.IUpdateFieldService;
import com.rykj.qxj.server.util.date.DateUtils;

import lombok.extern.log4j.Log4j;
@Log4j
@Component
public class AccessLimitInterceptor implements HandlerInterceptor {
	@Autowired
	private IApiManageService apiManageService;
	
	@Autowired
	private IDatabaseSourceService databaseSourceService;
	
	@Autowired
	private IApiRequestParamService apiRequestParamService;
	
	@Autowired
	private IApiResponseParamService apiResponseParamService;
	
	@Autowired
	private IUpdateFieldService updateFieldService;
	
	@Autowired
	private IAddFieldService addFieldService;
	
	@Autowired
	private ISystemParameterService systemParameterService;
	
	@Autowired
	private IApplyManageService applyManageService;
	
	@Autowired
	private IApiVersionManageService apiVersionManageService;
 	
	
	private static final String REQUEST_METHOD_GET = "GET";  //GET 请求
	private static final String REQUEST_METHOD_POST = "POST"; //POST请求
	private static final String REQUEST_METHOD_PUT = "PUT";    //PUT请求
	private static final String REQUEST_METHOD_DELETE = "DELETE";  //DELETE请求
	
	private static final String BUSINESS="business";  //业务库
	private static final String SUBJECT="subject";  //主题库
	
	private static final String  REGEXP = "\'";  //替换sql语句中的单引号表达式
	//单个接口的QPS
	private static final String SINGL_QUERIES_PER_SECOND="singlQueriesPerSecond";
	//全局所有接口的QPS
	private static final String GLOBAL_QUERIES_PER_SECOND="globalQueriesPerSecond";
	
	private static final String REQUEST_API_SERVICE_URL="request:api:service:url";
	
	private static final int MINUTE_EXPIRE=60;
	private static final int HOUR_EXPIRE=60*60;
	private static final int DAY_EXPIRE=24*60*60;
	
	private static final String LEVEL_ONE_LIMIT_PER_MINUTE="levelOneLimitPerMinute";
	private static final String LEVEL_ONE_LIMIT_PER_HOUR= "levelOneLimitPerHour";
	private static final String LEVEL_ONE_LIMIT_PER_DAY= "levelOneLimitPerDay";
	private static final String LEVEL_TWO_LIMIT_PER_MINUTE= "levelTwoLimitPerMinute";
	private static final String LEVEL_TWO_LIMIT_PER_HOUR ="levelTwoLimitPerHour";
	private static final String LEVEL_TWO_LIMIT_PER_DAY ="levelTwoLimitPerDay";
	private static final String LEVEL_THREE_LIMIT_PER_MINUTE= "levelThreeLimitPerMinute";
	private static final String LEVEL_THREE_LIMIT_PER_HOUR= "levelThreeLimitPerHour";
	private static final String LEVEL_THREE_LIMIT_PER_DAY ="levelThreeLimitPerDay";
	private static final String LEVEL_ONE_PER_PERCENTAGE= "levelOnePercentage";
	private static final String LEVEL_TWO_PER_PERCENTAGE= "levelTwoPercentage";
	private static final String LEVEL_THREE_PER_PERCENTAGE= "levelThreePercentage";
	private static final String ALL_API_VISITCOUNT_PER_MINUTE= "allApiVisitCountPerMinute";
	private static final String PER_API_VISITCOUNT= "perApiVisitCount";
	private static final String PER_APPLICATION_VISITCOUNT_TODAY= "perApplicationVisitCount_today";
	private static final String PER_APPLICATION_VISITCOUNT= "perApplicationVisitCount";
	
	private static final String API_TOTAL_VISITCOUNT= "apiTotalvisitCount";  //所有接口总的访问量
	private static final String API_TODAY_VISITCOUNT= "apiTodayvisitCount";   //今日接口访问量
	
	private static final String ALL_APPLICATION_PERMONTH= "allApplicationPerMonth";   //所有应用每月访问量
	    @Override
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	    	String appKey = request.getHeader("appKey");
	    	if(StringUtils.isBlank(appKey)) {
	    		appKey=request.getParameter("appKey");
	    		if(StringUtils.isBlank(appKey)) {
	    			output(response, "请求头中appKey不能为空!");
		    		return false;
	    		}
	    	}
	    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat sdfHour=new SimpleDateFormat("yyyy-MM-dd HH");
	    	SimpleDateFormat sdfToday=new SimpleDateFormat("yyyy-MM-dd"); 
	    	SimpleDateFormat sdfMonth=new SimpleDateFormat("yyyy-MM"); 
	    	//单个接口的访问量
	    	String apiKeyCachePrefix = request.getRequestURI().replace("/weather-api/", "").replaceAll("/", ":");
	    	String urlInDataBase = request.getRequestURI().replace("/weather-api", "");
	    	ApiManage apiManage = (ApiManage) CacheGet.getObject(apiKeyCachePrefix);
	    	if(null==apiManage) {
	    		apiManage=apiManageService.selectApiManageByUrl(urlInDataBase);
	    		if(null==apiManage) {
	    			output(response, "请求地址错误,请检查!");
		    		return false;
	    		}
				CachePut.putObject(apiKeyCachePrefix,apiManage,0);
	    	}
			Date nowDate =new Date();
	    	//统计每个应用每个接口每小时访问量
            String appLyInterface =appKey+"_"+apiKeyCachePrefix+"+"+sdfHour.format(nowDate);
			String appLyInterfaceValue = CacheGet.getValue(appLyInterface);
			CacheStr.incr(appLyInterface);
			//设置过期时间
			if(StringUtils.isEmpty(appLyInterfaceValue)||CacheExsite.getExpire(appLyInterface)==-1) {
				int seconds= DAY_EXPIRE*3-(DateUtils.getSecond(nowDate)+60*DateUtils.getHour(nowDate));
				CacheExsite.expire(appLyInterface,seconds);
			}

			//接口每分钟总的访问量
			String allApiVisitcountPerMinute =  sdf.format(nowDate);
			String allApiVisitcountPerMinuteValue = CacheGet.getValue(allApiVisitcountPerMinute);
			CacheStr.incr(allApiVisitcountPerMinute);
			if(StringUtils.isEmpty(allApiVisitcountPerMinuteValue)||CacheExsite.getExpire(allApiVisitcountPerMinute)==-1) {
				//3天过期
				CacheExsite.expire(allApiVisitcountPerMinute,60*60*24*3);
			}
	    	//统计每个应用访问量Top10
	    	CacheZSet.zincrby(PER_APPLICATION_VISITCOUNT, 1, appKey);
	    	//统计每个接口访问量Top10
	    	CacheZSet.zincrby(PER_API_VISITCOUNT, 1, apiKeyCachePrefix);
	    	//所有接口总的访问量
	    	CacheStr.incr(API_TOTAL_VISITCOUNT);
	    	//接口每日访问量
	    	CacheZSet.zincrby(API_TODAY_VISITCOUNT, 1, sdfToday.format(new Date()));
	    	
	    	//所有应用每月访问量
	    	CacheZSet.zincrby(ALL_APPLICATION_PERMONTH, 1, sdfMonth.format(new Date()));
	    	//每个接口当日访问量
			String key = "perApiVisitCount_" +appKey+ "_today" + ":" + sdfToday.format(new Date());
			double api_today = CacheZSet.zincrby(key, 1, apiKeyCachePrefix);
			if(api_today==-1||CacheExsite.getExpire(key)==-1) {
				CacheExsite.expire(key,3*24*60*60);
			}
			//每个应用当日访问量
			key = PER_APPLICATION_VISITCOUNT_TODAY + ":" + sdfToday.format(new Date());
			double apply_today = CacheZSet.zincrby(key, 1, appKey);
			if(apply_today==-1||CacheExsite.getExpire(key)==-1) {
				CacheExsite.expire(key,5*24*60*60);
			}
	    	//统计每一个应用的访问量
	    	String visitCountPerappKey = CacheGet.getValue(appKey);
	    	if(StringUtils.isEmpty(visitCountPerappKey)) {
	    		CachePut.putCache(appKey, Integer.toString(1), 365*30*24*60*60);
	    	}else {
	    		CachePut.putCache(appKey, Integer.toString(Integer.parseInt(visitCountPerappKey) + 1) , 365*30*24*60*60);
	    	}
	    	//统计应用每分钟，小时，天的访问量
	    	String cacheKeyMinute=new StringBuffer().append(appKey).append(":minute").toString();
	    	String cacheKeyHour=new StringBuffer().append(appKey).append(":hour").toString();
	    	String cacheKeyDay=new StringBuffer().append(appKey).append(":day").toString();
	    	//应用每分钟的访问量
	    	String appKeyMinuteValue = CacheGet.getValue(cacheKeyMinute);
			CacheStr.incr(cacheKeyMinute);
			int second =0;
	    	if(StringUtils.isEmpty(appKeyMinuteValue)||CacheExsite.getExpire(cacheKeyMinute)==-1) {
				second=60-DateUtils.getSecond(new Date());
	    		CacheExsite.expire(cacheKeyMinute,second);
	    	}

	    	//应用每小时的访问量
	    	String appKeyHourValue = CacheGet.getValue(cacheKeyHour);
			CacheStr.incr(cacheKeyHour);
			if(StringUtils.isEmpty(appKeyHourValue)||CacheExsite.getExpire(appKeyHourValue)==-1) {
				Date date = new Date();
				second=((60-DateUtils.getMinute(date))*60 - DateUtils.getSecond(date));
				CacheExsite.expire(cacheKeyHour,second);
			}
	    	//应用每天的访问量
	    	String appKeyDayValue = CacheGet.getValue(cacheKeyDay);
			CacheStr.incr(cacheKeyDay);
	    	if(StringUtils.isEmpty(appKeyDayValue)||CacheExsite.getExpire(appKeyDayValue)==-1) {
				Date date = new Date();
				second= ((24-DateUtils.getHour(date))*60-DateUtils.getMinute(date))*60-DateUtils.getSecond(date);
				CacheExsite.expire(cacheKeyDay,second);
	    	}
	    	//根据应用的appKey查询应用信息
	    	Apply apply = applyManageService.selectApplyByAppKey(appKey);
	    	if(apply==null) {
	    		output(response, "当前应用未发布上线，请联系管理员...");
				return false;
	    	}
			SystemParameter systemParameterLevelOnePercent = systemParameterService.selectSystemParameterByParamKey(LEVEL_ONE_PER_PERCENTAGE);
    		SystemParameter systemParameterLevelTwoPercent = systemParameterService.selectSystemParameterByParamKey(LEVEL_TWO_PER_PERCENTAGE);
    		SystemParameter systemParameterLevelThreePercent = systemParameterService.selectSystemParameterByParamKey(LEVEL_THREE_PER_PERCENTAGE);
    		Integer cacheKeyMinuteVisit=Integer.parseInt(CacheGet.getValue(cacheKeyMinute));
    		Integer cacheKeyHourVisit=Integer.parseInt(CacheGet.getValue(cacheKeyHour));
    		Integer cacheKeyDayVisit=Integer.parseInt(CacheGet.getValue(cacheKeyDay));
    		//接口一级
    		if(apiManage.getLevel().equals(LevelEnum.LEVELONE.getStatus())) {
	    		SystemParameter systemParameterMinuteCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_ONE_LIMIT_PER_MINUTE);
	    		SystemParameter systemParameterHourCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_ONE_LIMIT_PER_HOUR);
	    		SystemParameter systemParameterDayCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_ONE_LIMIT_PER_DAY);
	    		if(apply.getLevel().equals(LevelEnum.LEVELONE.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "一级接口访问量["+cacheKeyMinuteVisit+"],当前用户为[一级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
						return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "一级接口访问量["+cacheKeyHourVisit+"],当前用户为[一级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "一级接口访问量["+cacheKeyDayVisit+"],当前用户为[一级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    		if(apply.getLevel().equals(LevelEnum.LEVELTWO.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "一级接口访问量["+cacheKeyMinuteVisit+"],当前用户为[二级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
						return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "一级接口访问量["+cacheKeyHourVisit+"],当前用户为[二级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "一级接口访问量["+cacheKeyDayVisit+"],当前用户为[二级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    		if(apply.getLevel().equals(LevelEnum.LEVELTHREE.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "一级接口,当前用户为[三级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
						return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "一级接口,当前用户为[三级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "一级接口,当前用户为[三级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    	}
	    	//接口二级
	    	if(apiManage.getLevel().equals(LevelEnum.LEVELTWO.getStatus())) {
	    		SystemParameter systemParameterMinuteCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_TWO_LIMIT_PER_MINUTE);
	    		SystemParameter systemParameterHourCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_TWO_LIMIT_PER_HOUR);
	    		SystemParameter systemParameterDayCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_TWO_LIMIT_PER_DAY);
	    		if(apply.getLevel().equals(LevelEnum.LEVELONE.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "二级接口访问量["+cacheKeyMinuteVisit+"],当前用户为[一级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "二级接口访问量["+cacheKeyHourVisit+"],当前用户为[一级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "二级接口访问量["+cacheKeyDayVisit+"],当前用户为[一级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    		if(apply.getLevel().equals(LevelEnum.LEVELTWO.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "二级接口访问量["+cacheKeyMinuteVisit+"],当前用户为[二级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "二级接口访问量["+cacheKeyHourVisit+"],当前用户为[二级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "二级接口访问量["+cacheKeyDayVisit+"],当前用户为[二级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			
	    		}
	    		if(apply.getLevel().equals(LevelEnum.LEVELTHREE.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "二级接口,当前用户为[三级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "二级接口,当前用户为[三级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "二级接口,当前用户为[三级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    	}
	    	//接口三级
	    	if(apiManage.getLevel().equals(LevelEnum.LEVELTHREE.getStatus())) {
	    		SystemParameter systemParameterMinuteCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_THREE_LIMIT_PER_MINUTE);
	    		SystemParameter systemParameterHourCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_THREE_LIMIT_PER_HOUR);
	    		SystemParameter systemParameterDayCount = systemParameterService.selectSystemParameterByParamKey(LEVEL_THREE_LIMIT_PER_DAY);
	    		if(apply.getLevel().equals(LevelEnum.LEVELONE.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "三级接口访问量["+cacheKeyMinuteVisit+"],当前用户为[一级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "三级接口访问量["+cacheKeyHourVisit+"],当前用户为[一级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelOnePercent.getValue()/100) {
	    				output(response, "三级接口访问量["+cacheKeyDayVisit+"],当前用户为[一级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelOnePercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    		if(apply.getLevel().equals(LevelEnum.LEVELTWO.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "三级接口访问量["+cacheKeyMinuteVisit+"],当前用户为[二级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "三级接口访问量["+cacheKeyHourVisit+"],当前用户为[二级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelTwoPercent.getValue()/100) {
	    				output(response, "三级接口访问量["+cacheKeyDayVisit+"],当前用户为[二级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelTwoPercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    		if(apply.getLevel().equals(LevelEnum.LEVELTHREE.getStatus())) {
	    			//每分钟控制
	    			if(cacheKeyMinuteVisit>systemParameterMinuteCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "三级接口,当前用户为[三级用户]，该用户的请求每分钟最多有["+systemParameterMinuteCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一分钟以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每小时控制
	    			if(cacheKeyHourVisit>systemParameterHourCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "三级接口,当前用户为[三级用户]，该用户的请求每小时最多有["+systemParameterHourCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一小时以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    			//每天控制
	    			if(cacheKeyDayVisit>systemParameterDayCount.getValue()*systemParameterLevelThreePercent.getValue()/100) {
	    				output(response, "三级接口,当前用户为[三级用户]，该用户的请求每天最多有["+systemParameterDayCount.getValue()*systemParameterLevelThreePercent.getValue()/100+"]个请求,建议一天以后再发送请求或者增大每分钟接口的阈值!");
	    				return false;
	    			}
	    		}
	    	}
			return true;
	    }
	 
	    public void output(HttpServletResponse response, String msg) throws IOException {
	        response.setContentType("application/json;charset=UTF-8");
	        ServletOutputStream outputStream = null; 
	        try {
	            outputStream = response.getOutputStream();
	            outputStream.write(msg.getBytes("UTF-8"));
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            outputStream.flush();
	            outputStream.close();
	        }
	    }
	 
	    @Override
	    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	 
	    }
	 
	    @Override
	    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
	 
	    }
}
